<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/6/8
 */


function getRequest($url,$refer = '',$timeout = 10){
    $ssl = stripos($url, 'https://') === 0 ? true : true;
    $curlObj = curl_init();
    $options = [
        CURLOPT_URL =>$url,
        CURLOPT_RETURNTRANSFER =>1,
        CURLOPT_FOLLOWLOCATION =>1,
        CURLOPT_AUTOREFERER =>1,
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_HTTP_VERSION=>CURL_HTTP_VERSION_1_0,
        CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
    ];

    if($refer){
        $options[CURLOPT_REFERER] = $refer;
    }

    if($ssl){
        $options[CURLOPT_SSL_VERIFYHOST] = false;
        $options[CURLOPT_SSL_VERIFYPEER] = false;
    }

    curl_setopt_array($curlObj,$options);
    $returnData = curl_exec($curlObj);
    if(curl_errno($curlObj)){
        $returnData = curl_error($curlObj);
    }

    curl_close($curlObj);
    return $returnData;

}

$urls = [
    'http://172.18.8.242:4999/web/#/5?page_id=126',
    'https://www.php.net/manual/zh/reflectionmethod.invoke.php',
    'https://www.php.net/manual/zh/reflectionmethod.getprototype.php',
    'https://www.php.net/manual/zh/reflectionmethod.isstatic.php',
    'https://www.php.net/manual/zh/funcref.php'
];

$data = [];
foreach ($urls as $url){
    $data[] = getRequest($url);
}

/**
 * @link https://segmentfault.com/a/1190000016343861?utm_source=sf-similar-article
 * @param $urls
 * @return array
 * @author Zhoufurong
 * @Date 2021/6/8
 */
function multiRequest($urls){
    $mh = curl_multi_init();
    $urlHandlers = [];
    $urlData = [];

    foreach ($urls as $value){
        $ch = curl_init();
        $url = $value;
        $url .=strpos($url,'?') ? '&':'?';
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
        $urlHandlers[] = $ch;
        curl_multi_add_handle($mh,$ch);
    }

    $active = null;

    do {
        $mrc = curl_multi_exec($mh,$active);
    }while($mrc == CURLM_CALL_MULTI_PERFORM);

    while($active && $mrc == CURLM_OK){
        usleep(50000);

        if(curl_multi_select($mh) != -1){
            do {
                $mrc = curl_multi_exec($mh,$active);
            }while($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    foreach ($urlHandlers as $index =>$ch){
        $urlData[$index] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh,$ch);
    }
    curl_multi_close($mh);
    return $urlData;
}
